<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="微信 openid" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入微信 openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工号" prop="employeeId">
        <el-input
          v-model="queryParams.employeeId"
          placeholder="请输入工号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否自定义号段" prop="isCustom">
        <el-select
          v-model="queryParams.isCustom"
          placeholder="请选择"
          clearable
        >
          <el-option label="否" :value="0" />
          <el-option label="是" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分配号码" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入分配号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="奖励分值" prop="reward">
        <el-input
          v-model="queryParams.reward"
          placeholder="请输入奖励分值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate(selectionRow)"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- 批量删除，用在表格上方 -->
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="ids.length === 0"
          @click="handleBatchDelete"
        >批量删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自增主键" prop="id" align="center" />
      <el-table-column label="微信 openid" prop="openid" align="center" />
      <el-table-column label="名称" prop="name" align="center" />
      <el-table-column label="工号" prop="employeeId" align="center" />
      <el-table-column label="自定义号段" prop="isCustom" align="center">
        <template #default="{ row }">
          {{ row.isCustom === 1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="级别" prop="level" align="center" />
      <el-table-column label="分配号码" prop="number" align="center" />
      <el-table-column label="备注" prop="remark" align="center" />
      <el-table-column label="奖励分值" prop="reward" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            v-hasPermi="['system:info:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="微信 openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入微信 openid" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="工号" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入工号" />
        </el-form-item>
        <el-form-item label="是否自定义号段" prop="isCustom">
          <el-select v-model="form.isCustom" placeholder="请选择">
            <el-option label="否" :value="0" />
            <el-option label="是" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
        <el-form-item label="分配号码" prop="number">
          <el-input v-model="form.number" placeholder="请输入分配号码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="奖励分值" prop="reward">
          <el-input v-model="form.reward" placeholder="请输入奖励分值" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
} from "@/api/system/info";

export default {
  name: "Info",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      selectionRow: null,
      showSearch: true,
      total: 0,
      infoList: [],
      open: false,
      title: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openid: null,
        name: null,
        employeeId: null,
        isCustom: null,
        level: null,
        number: null,
        remark: null,
        reward: null,
      },
      form: {},
      rules: {
        openid: [{ required: true, message: "微信 openid 不能为空", trigger: "blur" }],
        employeeId: [{ required: true, message: "工号不能为空", trigger: "blur" }],
        isCustom: [{ required: true, message: "是否自定义号段不能为空", trigger: "blur" }],
        level: [{ required: true, message: "级别不能为空", trigger: "blur" }],
        number: [{ required: true, message: "分配号码不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleBatchDelete() {
      if (!this.ids.length) return;
      this.$modal
        .confirm(`确定批量删除编号为 ${this.ids.join(',')} 吗？`)
        .then(() => delInfo(this.ids))
        .then(() => {
          this.$modal.msgSuccess("批量删除成功");
          this.getList();
        });
    },
    getList(pagination) {
      this.loading = true;
      // 如果传入了分页参数，更新查询参数
      if (pagination) {
        this.queryParams.pageNum = pagination.page;
        this.queryParams.pageSize = pagination.limit;
      }
      listInfo(this.queryParams).then((res) => {
        this.infoList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      
    },
    handleAdd() {
      this.form = {};
      this.title = "添加用户分配号码";
      this.open = true;
    },
    handleUpdate(row) {
      const target = row || this.selectionRow;
      if (!target) return;
      getInfo(target.id).then((res) => {
        this.form = res.data;
        this.title = "修改用户分配号码";
        this.open = true;
      });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (!valid) return;
        const fn = this.form.id != null ? updateInfo : addInfo;
        fn(this.form).then(() => {
          this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    handleDelete(row) {
      const ids = (row && row.id) || this.ids;
      this.$modal
        .confirm(`是否确认删除编号为"${ids}"的数据？`)
        .then(() => delInfo(ids))
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => {
        });
    },
    handleExport() {
      this.download(
        "system/info/export",
        {...this.queryParams},
        `info_${Date.now()}.xlsx`
      );
    },
    cancel() {
      this.open = false;
    },
  },
};
</script>
